<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'form', 'array', 'captcha'));
        $this->load->library(array('country', 'form_validation', 'recaptcha'));
        $this->load->model(array('user_model', 'setting_model'));
    }

    public function index() {
        $styles = array(
            'js/bootstrap/dist/css/bootstrap.css',
            'fonts/font-awesome-4/css/font-awesome.min.css',
            'js/jquery.gritter/css/jquery.gritter.css',
            'js/jquery.nanoscroller/nanoscroller.css',
            'js/jquery.easypiechart/jquery.easy-pie-chart.css',
            'js/bootstrap.switch/bootstrap-switch.css',
            'js/bootstrap.datetimepicker/css/bootstrap-datetimepicker.min.css',
            'js/jquery.select2/select2.css',
            'js/bootstrap.slider/css/slider.css',
            'js/intro.js/introjs.css',
            'css/style.css',
            'css/ts-style.css',
        );
        $scripts = array(
            'js/jquery.js',
            'js/jquery.gritter/js/jquery.gritter.js',
            'js/jquery.nanoscroller/jquery.nanoscroller.js',
            'js/behaviour/general.js',
            'js/jquery.ui/jquery-ui.js',
            'js/jquery.sparkline/jquery.sparkline.min.js',
            'js/jquery.easypiechart/jquery.easy-pie-chart.js',
            'js/jquery.nestable/jquery.nestable.js',
            'js/bootstrap.switch/bootstrap-switch.min.js',
            'js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js',
            'js/jquery.select2/select2.min.js',
            'js/skycons/skycons.js',
            'js/bootstrap.slider/js/bootstrap-slider.js',
            'js/intro.js/intro.js',
            'https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false',
            'js/page/index.js',
            'js/behaviour/voice-commands.js',
            'js/bootstrap/dist/js/bootstrap.min.js',
            'js/jquery.flot/jquery.flot.js',
            'js/jquery.flot/jquery.flot.pie.js',
            'js/jquery.flot/jquery.flot.resize.js',
            'js/jquery.flot/jquery.flot.labels.js',
        );
        $data = array(
            'assets' => base_url() . 'assets',
            'title' => 'Index',
            'user_info' => $this->session->userdata('user_info'),
            'styles' => $styles,
            'scripts' => $scripts,
        );

        $this->load->view('include/header', $data);
        $this->load->view('home/index', $data);
        $this->load->view('include/footer', $data);
    }

    public function register() {
        $avatarurl = "http://168.144.171.228:8087/Image?Name=Avatars";   // set your url here
        $avatarmax = 64;       // number of avatars available
        //STYLES
        $styles = array(
            'js/bootstrap/dist/css/bootstrap.css',
            'js/bootstrap.datetimepicker/css/bootstrap-datetimepicker.min.css',
            'js/jquery.select2/select2.css',
            'js/bootstrap.slider/css/slider.css',
            'js/fuelux/css/fuelux.css',
            'js/fuelux/css/fuelux-responsive.min.css',
            'fonts/font-awesome-4/css/font-awesome.min.css',
            'css/pygments.css',
            'js/jquery.nanoscroller/nanoscroller.css',
            'js/bootstrap.switch/bootstrap-switch.css',
            'css/style.css',
            'css/ts-style.css',
            'js/jquery.icheck/skins/square/blue.css',
            'js/jquery.gritter/css/jquery.gritter.css',
        );

        //SCRIPTS
        $scripts = array(
            'js/jquery.js',
            'js/jquery.select2/select2.min.js',
            'js/bootstrap.slider/js/bootstrap-slider.js',
            'js/fuelux/loader.min.js',
            'js/modernizr.js',
            'js/jquery.nanoscroller/jquery.nanoscroller.js',
            'js/bootstrap.switch/bootstrap-switch.min.js',
            'js/jquery.nestable/jquery.nestable.js',
            'js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js',
            'js/behaviour/general.js',
            'js/jquery.ui/jquery-ui.js',
            'js/page/register.js',
            'js/behaviour/voice-commands.js',
            'js/jquery.icheck/icheck.min.js',
            'js/jquery.gritter/js/jquery.gritter.js',
            'js/ts.js',
        );

        //DATA
        $user_ip = $this->country->get_user_ip();
        $ip_info = $this->country->get_user_info();
        $data = array(
            'assets' => base_url() . 'assets',
            'title' => 'Register',
            'user_info' => $this->session->userdata('user_info'),
            'avatarurl' => $avatarurl,
            'avatarmax' => $avatarmax,
            'styles' => $styles,
            'scripts' => $scripts,
            'user_ip' => $user_ip,
            'ip_info' => $ip_info,
            'recaptcha' => $this->recaptcha->get_html('recaptcha/recaptcha'),
        );

        if ($data['user_info'] !== FALSE) {
            redirect('home/index');
        }

        $this->load->view('include/header', $data);
        $this->load->view('home/register', $data);
        $this->load->view('include/footer', $data);
    }

    public function do_register() {
        $avatarurl = "http://168.144.171.228:8087/Image?Name=Avatars";   // set your url here
        $avatarmax = 64;       // number of avatars available
        //
		//STYLES
        $styles = array(
            'js/bootstrap/dist/css/bootstrap.css',
            'js/bootstrap.datetimepicker/css/bootstrap-datetimepicker.min.css',
            'js/jquery.select2/select2.css',
            'js/bootstrap.slider/css/slider.css',
            'js/fuelux/css/fuelux.css',
            'js/fuelux/css/fuelux-responsive.min.css',
            'fonts/font-awesome-4/css/font-awesome.min.css',
            'css/pygments.css',
            'js/jquery.nanoscroller/nanoscroller.css',
            'js/bootstrap.switch/bootstrap-switch.css',
            'css/style.css',
            'css/ts-style.css',
            'js/jquery.icheck/skins/square/blue.css',
            'js/jquery.gritter/css/jquery.gritter.css',
        );

        //SCRIPTS
        $scripts = array(
            'js/jquery.js',
            'js/jquery.select2/select2.min.js',
            'js/bootstrap.slider/js/bootstrap-slider.js',
            'js/fuelux/loader.min.js',
            'js/modernizr.js',
            'js/jquery.nanoscroller/jquery.nanoscroller.js',
            'js/bootstrap.switch/bootstrap-switch.min.js',
            'js/jquery.nestable/jquery.nestable.js',
            'js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js',
            'js/behaviour/general.js',
            'js/jquery.ui/jquery-ui.js',
            'js/page/register.js',
            'js/behaviour/voice-commands.js',
            'js/jquery.icheck/icheck.min.js',
            'js/jquery.gritter/js/jquery.gritter.js',
            'js/ts.js',
        );

        //DATA
        $user_ip = $this->country->get_user_ip();
        $ip_info = $this->country->get_user_info();

        $data = array(
            'assets' => base_url() . 'assets',
            'title' => 'Register',
            'user_info' => $this->session->userdata('user_info'),
            'avatarurl' => $avatarurl,
            'avatarmax' => $avatarmax,
            'styles' => $styles,
            'scripts' => $scripts,
            'user_ip' => $user_ip,
            'ip_info' => $ip_info,
            'success' => FALSE,
            'recaptcha' => $this->recaptcha->get_html('recaptcha/recaptcha'),
        );

        //VALIDATION
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[12]|is_unique[user.username]|alpha_dash');
        $this->form_validation->set_rules('nickname', 'Nick name', 'trim|required|max_length[15]|is_unique[user.nickname]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|max_length[80]|valid_email|is_unique[user.email]|callback_check_exist[' . $this->input->post('username') . ']');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        $this->form_validation->set_rules('retype_password', 'Verify Password', 'trim|required|matches[password]');
        $this->form_validation->set_rules('bonus_code', 'Bonus Code', 'trim');
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|callback_check_realname[' . $this->input->post('first_name') . ']');
        $this->form_validation->set_rules('phone_code', 'Phone Code', 'trim|required|numeric');
        $this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
        $this->form_validation->set_rules('street_address', 'Street Address', 'trim|required');
        $this->form_validation->set_rules('city', 'City', 'trim|required');
        $this->form_validation->set_rules('state', 'State', 'trim|required|callback_check_location[' . $this->input->post('city') . ']');
        $this->form_validation->set_rules('zip_code', 'Zip Code', 'trim|required|numeric');
        $this->form_validation->set_rules('answer', 'Answer', 'trim');
        $this->form_validation->set_rules('recaptcha_response_field', 'Captcha', 'required|callback_check_captcha');

        $this->form_validation->set_message('is_unique', 'The %s you entered is already existed.');

        if ($this->form_validation->run() === FALSE) {
            $data['success'] = FALSE;

            $this->load->view('include/header', $data);
            $this->load->view('home/register', $data);
            $this->load->view('include/footer', $data);
        } else {
            $post_array = $this->input->post();
            $user = elements(array('username', 'nickname', 'email', 'password', 'bonus_code', 'avatar', 'first_name', 'last_name', 'gender', 'phone_code', 'phone', 'ip', 'country', 'city', 'state', 'street_address', 'zip_code', 'answer'), $post_array, '');

            $this->user_model->insert($user);
            $account = elements(array('username', 'nickname', 'first_name', 'last_name', 'password', 'city', 'state', 'email', 'avatar', 'gender'), $post_array);
            Api::add_account($account);

            $data['success'] = TRUE;

            $this->load->view('include/header', $data);
            $this->load->view('home/register_success', $data);
            $this->load->view('include/footer', $data);
        }
    }

    public function login() {
        $data = array(
            'assets' => base_url() . 'assets',
            'title' => 'Login',
            'user_info' => $this->session->userdata('user_info'),
        );

        if ($data['user_info'] !== FALSE) {
            redirect('home/index');
        }

        $this->load->view('home/login', $data);
    }

    public function do_login() {

    }

    public function test() {
        $styles = array(
            'js/bootstrap/dist/css/bootstrap.css',
            'fonts/font-awesome-4/css/font-awesome.min.css',
            'js/jquery.gritter/css/jquery.gritter.css',
            'js/jquery.nanoscroller/nanoscroller.css',
            'js/jquery.easypiechart/jquery.easy-pie-chart.css',
            'js/bootstrap.switch/bootstrap-switch.css',
            'js/bootstrap.datetimepicker/css/bootstrap-datetimepicker.min.css',
            'js/jquery.select2/select2.css',
            'js/bootstrap.slider/css/slider.css',
            'js/intro.js/introjs.css',
            'css/style.css',
            'css/ts-style.css',
        );
        $scripts = array(
            'js/jquery.js',
            'js/jquery.gritter/js/jquery.gritter.js',
            'js/jquery.nanoscroller/jquery.nanoscroller.js',
            'js/behaviour/general.js',
            'js/jquery.ui/jquery-ui.js',
            'js/jquery.sparkline/jquery.sparkline.min.js',
            'js/jquery.easypiechart/jquery.easy-pie-chart.js',
            'js/jquery.nestable/jquery.nestable.js',
            'js/bootstrap.switch/bootstrap-switch.min.js',
            'js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js',
            'js/jquery.select2/select2.min.js',
            'js/skycons/skycons.js',
            'js/bootstrap.slider/js/bootstrap-slider.js',
            'js/intro.js/intro.js',
            'https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false',
            'js/page/index.js',
            'js/behaviour/voice-commands.js',
            'js/bootstrap/dist/js/bootstrap.min.js',
            'js/jquery.flot/jquery.flot.js',
            'js/jquery.flot/jquery.flot.pie.js',
            'js/jquery.flot/jquery.flot.resize.js',
            'js/jquery.flot/jquery.flot.labels.js',
        );

        $data = array(
            'assets' => base_url() . 'assets',
            'title' => 'Test',
            'user_info' => $this->session->userdata('user_info'),
            'styles' => $styles,
            'scripts' => $scripts,
        );
        $this->load->view('include/header', $data);
        $this->load->view('home/test', $data);
        $this->load->view('include/footer', $data);
    }

    public function check_realname($last_name, $first_name) {
        if (strlen($first_name) + strlen($last_name) > 24) {
            $this->form_validation->set_message('check_realname', 'Your real name (first name + last name) must be under 23 characters');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function check_location($state, $city) {
        if (strlen($state) + strlen($city) > 28) {
            $this->form_validation->set_message('check_location', 'Your location (city + state) must be under 28 characters');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function check_exist($email, $username) {
        $value = array(
            'username' => $username,
            'email' => $email,
        );
        $rs = Api::check_account($value);
        if ($rs === NULL) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_exist', 'Your ' . $rs . ' is existed and not available!');
            return FALSE;
        }
    }

    public function check_captcha($value) {
        if ($this->recaptcha->check_answer($_SERVER['REMOTE_ADDR'], $this->input->post('recaptcha_challenge_field'), $value)) {
            return TRUE;
        } else {
            $this->form_validation->set_message(__FUNCTION__, 'Incorrect captcha response');
            return FALSE;
        }
    }

}

/* End of file home.php */
/* Location: ./application/controllers/home.php */